package com.eqdd.yiqidian.kotlin.utils

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.support.design.widget.BottomSheetDialog
import android.view.LayoutInflater
import android.view.View
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpResult
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-9-12.
 */
object DialogUtil {

    fun delete(context: BaseActivity, url: String, msg: String = "是否删除该数据？", isFinish: Boolean, vararg params: Pair<String, Any?>, body: () -> Unit = {}) {
        val httpParams = HttpParams()
        params.forEach {
            httpParams.put(it.first, it.second?.toString())
        }

        AlertDialog.Builder(context)
                .setTitle("提示")
                .setMessage(msg)
                .setPositiveButton("取消", null)
                .setNegativeButton("确定", { _, _ ->
                    OkGo.post<HttpResult<*>>(url)
                            .params("userGuid", context.user.guid)
                            .params("companyId", context.user.companyId)
                            .params(httpParams)
                            .execute(object : DialogCallBack<HttpResult<*>>(context) {
                                override fun onSuccess(response: Response<HttpResult<*>>?) {
                                    val result = response?.body()
                                    result?.let {
                                        if (it.status == 200) {
                                            if (isFinish) {
                                                context.toast("删除成功")
                                                context.setResult(Activity.RESULT_OK)
                                                context.finish()
                                            }
                                            body()
                                        } else {
                                            context.toast(it.msg)
                                        }
                                    }
                                }
                            })
                })
                .show()
    }

    fun bottomCustom(context: Context, layoutRes: Int, body: (v: View, dialog: BottomSheetDialog) -> Unit) {
        val dialog = BottomSheetDialog(context)
        val view = LayoutInflater.from(context).inflate(layoutRes, null, false)
        dialog.setContentView(view)
        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()
        body(view, dialog)
    }


}